package com.ybg.engine.task;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.ybg.util.CSVUtils;
import com.ybg.util.LogUtil;
 /**
  * 读取签到数据接口任务
 * @author huang nc
 * @since  2017年4月18日
 */
public class CreateCsvFileTask extends Task {

	private static boolean running = false;
    public List<Map<String,Object>> exportData = new ArrayList<Map<String,Object>>();
    public LinkedHashMap<String, Object> map = new LinkedHashMap<String, Object>();
    
	@Override
	public void run() throws Exception {
	 if (!enter()){
            throw new RuntimeException("调度已经在进行中");
        }
        LogUtil.warn("teacher-course-stati...begin........");
        long begin = new Date().getTime();

        System.out.println("你看到我了吗？");
        CreateCSVData();
        //TaskUtil.markProcess(jdbcTemplate, TaskName.UPDATE_STATUS, DateTimeUtil.getToday(DateTimeUtil.YYYY_MM_DD_HH_MM_SS));
        long end = new Date().getTime();
        LogUtil.info("本次任务用时 :" + (end - begin) / 1000 + "秒");
        LogUtil.warn("teacher-course-stati...begin........");
        leaving();
	}

	//重新导入课表数据
	private void CreateCSVData() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); 
	    String todayTime = sdf.format(new Date());
	    List<String> list = new ArrayList<String>();
	    list.add("%"+todayTime+"%");
		String sqlString = "select XN,XQ,QSJSZ,DJJ,SKDD,KCDM,KCMC,GH,RKLS,XH,XM,BJID,BJMC,DMZT,DQZT,YCYY,DMSJ,QDSJ,DJZ,XQJ from YBG_B_SIGN where dmsj like ? ";
		exportData = jdbcTemplate.queryForList(sqlString,list.toArray());
		
		map.put("1", "XN");  
	    map.put("2", "XQ");  
	    map.put("3", "QSJSZ");  
	    map.put("4", "DJJ");
	    map.put("5", "SKDD");
	    map.put("6", "KCDM"); 
	    map.put("7", "KCMC"); 
	    map.put("8", "GH"); 
	    map.put("9", "RKLS"); 
	    map.put("10", "XH"); 
	    map.put("11", "XM"); 
	    map.put("12", "BJID"); 
	    map.put("13", "BJMC"); 
	    map.put("14", "DMZT"); 
	    map.put("15", "DQZT"); 
	    map.put("16", "YCYY"); 
	    map.put("17", "DMSJ"); 
	    map.put("18", "QDSJ"); 
	    map.put("19", "DJZ"); 
	    map.put("20", "XQJ");
	    
	    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");  
	    String str = simpleDateFormat.format(new Date());

	    // String path = "d:/export/";  
	    String path = "/opt/ybg/jobengine/csvfile/";
		String fileName = "schoolName"+str+".csv";  
	    File file = CSVUtils.createCSVFile(exportData, map, path, fileName);  
	   /* String fileName2 = file.getName();  
	    System.out.println("文件名称：" + fileName2); */ 
	}

	private synchronized boolean enter(){
        if(running){
            return false;
        }else{
            running = true;
            return true;
        }
    }

    private synchronized void leaving(){
        running = false;
    }

	@Override
	public String setSuccessResultMsg() {
		
		return "生成CSV文件数据调度成功";
	}

}
